<template>
  <div class="container">
    <!-- 固定导航 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小思同学"
    ></van-nav-bar>

    <!-- 聊天主体区域 -->
    <div class="chat-list">
      <div
        v-for="(item,index) in myList"
        :key="index"
        ref="chat-item"
      >
        <!-- 左侧是机器人小思 -->
        <div
          class="chat-item left"
          v-if="item.name==='xs'"
        >
          <van-image
            fit="cover"
            round
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          />
          <div class="chat-pao">{{item.msg}}</div>
        </div>

        <!-- 右侧是当前用户 -->
        <div
          ref="chat-item"
          class="chat-item right"
          v-if="item.name==='my'"
        >
          <div class="chat-pao">{{item.myMsg}}</div>
          <van-image
            fit="cover"
            round
            :src="photo"
          />
        </div>
      </div>
    </div>

    <!-- 对话区域 -->
    <div class="reply-container van-hairline--top">
      <van-field
        placeholder="说点什么..."
        v-model='val'
        @keydown.enter="send(val)"
      >
        <template #button>
          <span
            style="font-size:12px;color:#999"
            @click="send(val)"
          >提交</span>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { getPersonalInformation } from "@/api/my.js";
import { io } from 'socket.io-client'
let socket = null
//导入局部组件必须在scrip里面导入,在App里面导入
export default {
  // 组件名称
  name: 'chat',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 接收局部注册的组件
  components: {},
  // 组件数据
  data () {
    return {
      photo: null,//用户头像
      myList: [{ msg: '我是小思', name: 'xs' }],//自己说的话
      val: '',//输入框的值
    }
  },
  methods: {
    send (val) {
      if (this.val == '') {
        this.$toast.fail('请输入内容')
        return
      }

      this.myList.push({ myMsg: val, name: 'my' })
      this.val = ''
      // 异步更新
      this.$nextTick(() => {
        this.collet()
      })
      // 发送
      socket.emit('message', {
        msg: val,
        timestamp: Date.now()
      })
    },
    collet () {
      // 找到refs的div
      const chatem = this.$refs['chat-item']
      //找到最后一个div
      const chatitme = chatem[chatem.length - 1]
      chatitme.scrollIntoView({
        behavior: 'smooth'
      })
    }
  },
  async created () {

    // 获取发送人的头像
    let res = await getPersonalInformation()
    this.photo = res.data.data.photo

    //   创建实例
    socket = io('http://toutiao.itheima.net', {
      query: {
        //代上自己的token
        token: window.localStorage.getItem('token')
      },
      transports: ['websocket']
    })
    //接收 data 是后端返回的数据
    socket.on('connect', () => {
      socket.on('message', data => {
        // console.log(data);
        this.myList.push({ ...data, name: 'xs' })
        this.$nextTick(() => {
          this.collet()
        })
      })
    })

  },
  beforeDestroy () {
    socket.close() //关闭链接
    socket = null //重置为空
  }
}
</script>
<style lang='less' scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>